package com.atg.atgchat.init.common.event.listener;

import com.atg.atgchat.init.common.event.UserRegisteredEvent;
import com.atg.atgchat.init.user.model.entity.User;
import com.atg.atgchat.init.user.model.enums.IdempotentTypeEnum;
import com.atg.atgchat.init.user.model.enums.ItemEnum;
import com.atg.atgchat.init.user.service.UserBackpackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/*
author: atg
time: 2025/1/17 15:53
*/
@Slf4j
@Component
public class UserRegisterListener {
    @Resource
    private UserBackpackService userBackpackService;

    @Async
//    @EventListener(classes = UserRegisteredEvent.class)
    @TransactionalEventListener(classes = UserRegisteredEvent.class, phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true)
    public void sendCard(UserRegisteredEvent event) {
        User user = event.getUser();
        userBackpackService.acquireItem(user.getId(), ItemEnum.MODIFY_NAME_CARD.getId(), IdempotentTypeEnum.UID, user.getId().toString());
        log.info("用户注册成功，用户id为：{}", event.getUser().getId());
    }
}
